const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = (env) => {
  return {
    mode: env.production ? 'production' : 'development',
    entry: './src/index.js',
    output: {
      filename: 'BeatifIntredPlayer.min.js',
      path: path.resolve(__dirname, 'dist'),
      library: {
        name: 'BeatifIntredPlayer',
        type: 'umd',
        export: 'default'
      },
      globalObject: 'this'
    },
    optimization: {
      minimize: true,
      usedExports: false, // 禁用tree-shaking测试
      concatenateModules: false,
      sideEffects: false
    },
    stats: {
        usedExports: true,
        optimizationBailout: true,
        providedExports: true
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          include: [
            path.resolve(__dirname, 'src'),
            path.resolve(__dirname, 'node_modules/your-other-modules')
          ],
          use: {
            loader: 'babel-loader',
            options: {
              presets: ['@babel/preset-env'],
              plugins: ['@babel/plugin-transform-runtime']
            }
          }
        },
        {
          test: /\.css$/,
          use: [
            MiniCssExtractPlugin.loader,
            'css-loader',
            'postcss-loader'
          ]
        }
      ]
    },
    plugins: [
      new MiniCssExtractPlugin({
        filename: 'BeatifIntredPlayer.min.css'
      }),
      new webpack.DefinePlugin({
        'process.env.VERSION': JSON.stringify(env.BUILD_VERSION || '1.0.0')
      }),
      new webpack.optimize.LimitChunkCountPlugin({
        maxChunks: 1 // 强制单文件输出
      })
    ],
    externals: [
      {
        'flv.js': 'flvjs',
        'hls.js': 'Hls',
        'jquery': 'jQuery'
      },
      // 其他外部依赖
      /^lodash\/.+$/ // 示例：排除lodash
    ]
  };
};
